package cn.tedu.charging.user.controller;

import cn.tedu.charging.common.pojo.param.ChargeParam;
import cn.tedu.charging.common.pojo.param.VehicleBindParam;
import cn.tedu.charging.common.pojo.vo.VehicleVO;
import cn.tedu.charging.common.protocol.JsonResult;
import cn.tedu.charging.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @GetMapping("/user/wx/login")
    public JsonResult wxLogin(String code,String nickname){
        Integer userId=userService.wxLogin(code,nickname);
        return JsonResult.ok(userId);
    }
    @GetMapping("/user/vehicle/binded/{userId}")
    public JsonResult getBindedVehicle(@PathVariable("userId")Integer userId){
        log.debug("获取用户绑定的车辆：");
        VehicleVO vehicleVO=userService.getBindedVehicle(userId);
        log.debug("获取用户绑定的车辆："+vehicleVO);
        return JsonResult.ok(vehicleVO);
    }

    @PostMapping("/user/vehicle/bind")
    public JsonResult<Integer> bindVehicle(@RequestBody VehicleBindParam param){
        log.debug("绑定用户车辆");
        Integer vehicleId = userService.bindVehicle(param);
        return JsonResult.ok(vehicleId);
    }
    @GetMapping("/user/vehicle/unbind")
    public JsonResult unbindVehicle(Integer userId){
        log.debug("解绑用户车辆");
        userService.unbindVehicle(userId);
        return JsonResult.ok(null);
    }
    @GetMapping("/user/balance/{userId}")
    public JsonResult getBalance(@PathVariable("userId")Integer userId){
        log.debug("获取用户余额");
        Double balance = userService.Balance(userId);
        return JsonResult.ok(balance);
    }

    @PostMapping("/user/charge")
    public JsonResult charge(@RequestBody ChargeParam param){
        log.debug("用户充值");
        userService.charge(param);
        return JsonResult.ok(null);
    }
}
